package net.stogi.unicore.beyond.ui.client.taskeditor.editormenu;

import net.stogi.common.mvp.core.behavior.Behavior;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;

class EditorMenuWidget extends Composite implements EditorMenuView {

	private final ContentPanel mainPanel = new ContentPanel();

	private final Button newTaskButton = new Button();
	
	private final Button openTaskButton = new Button();
	
	private final Button saveTaskButton = new Button();
	
	public EditorMenuWidget() {
		initComposite();
		initI18n();
		initLayout();
		initComponent(mainPanel);
	}

	@Override
	public void blockSaveTask() {
		saveTaskButton.setEnabled(false);
	}

	@Override
	public void setNewTaskBehavior(final Behavior behavior) {
		newTaskButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setOpenTaskBehavior(final Behavior behavior) {
		openTaskButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setSaveTaskBehavior(final Behavior behavior) {
		saveTaskButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void unblockSaveTask() {
		saveTaskButton.setEnabled(true);
	}

	private void initComposite() {
		mainPanel.setFrame(true);
		saveTaskButton.setEnabled(false);
		
		newTaskButton.setWidth("100%");
		openTaskButton.setWidth("100%");
		saveTaskButton.setWidth("100%");
	}

	private void initI18n() {
		mainPanel.setHeading("Editor Menu");
		newTaskButton.setText("New Task");
		openTaskButton.setText("Open Task");
		saveTaskButton.setText("Save Task");
	}

	private void initLayout() {
		mainPanel.add(newTaskButton);
		mainPanel.add(openTaskButton);
		mainPanel.add(saveTaskButton);
	}
}
